package com.WISSC.serviceImpl;

import com.WISSC.bean.*;
import com.WISSC.bean.layuiBean.ResultPage;
import com.WISSC.mapper.StudentMapper;
import com.WISSC.service.StudentImageService;
import com.WISSC.service.StudentService;
import com.WISSC.util.Respons.ResponseUtil;
import com.WISSC.util.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学员信息 服务实现类
 * </p>
 *
 * @author 张凯
 * @since 2019-05-05
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {


    @Autowired
    StudentImageService studentImageService;


    /**
     * @param current
     * @param size
     * @param name
     * @param idNumber
     * @param regTime
     * @param city
     * @param subject
     * @param subjectLevel
     * @return 查询 + 分页
     */
    @Override
    public ResultPage<Student> selectMyPage(int current, int size, String name, String idNumber, Long regTime, String city, Integer subject, Integer subjectLevel,boolean admin) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (current - 1) * size);
        paramMap.put("limit", size);
        paramMap.put("name", name);
        paramMap.put("idNumber", idNumber);
        paramMap.put("regTime", regTime);
        paramMap.put("city", city);
        paramMap.put("subject", subject);
        paramMap.put("subjectLevel", subjectLevel);
        List<Student> list = baseMapper.selectStudent(paramMap,admin);
        Integer count = baseMapper.countStudent(paramMap,admin);
        ResultPage<Student> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }


    /**
     * @param student
     * @param pIds
     * @return 学员信息添加
     * @throws SQLException
     */
    @Override
    public String studentInsert(Student student, String[] pIds, String param2){
        Map<String, Object> map = new HashMap<String, Object>();
        String[] str = param2.split("@");
        ArrayList list = new ArrayList();
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        if (list.size() != pIds.length) {
            for (int i = 0; i < pIds.length; i++) {
                studentImageService.removeById(pIds[i]);
            }
            map.put("flag", false);
            map.put("msg", "添加失败");
            return JSON.toJSONString(map);
        } else {
            //创建日期
            student.setCreateTime(DateUtil.date());
            if (this.save(student)) {
                for (int i = 0; i < list.size(); i++) {
                    String arr = (String) list.get(i);
                    String[] ss = arr.split(",");
                    if (ss != null) {
                        for (int j = 0; j < 1; j++) {
                            StudentImage studentImage = new StudentImage();
                            studentImage.setId(Long.parseLong(pIds[i]));
                            if (ss[0].equals("undefined")) {
                                studentImageService.removeById(studentImage);
                                break;
                            }
                            studentImage.setSubject(Integer.parseInt(ss[0]));
                            studentImage.setSubjectLevel(Integer.parseInt(ss[1]));
                            studentImage.setIdNo(ss[2]);
                            studentImage.setCreateTime(DateUtil.date());
                            studentImage.setStudentId(student.getId());
                            studentImageService.updateById(studentImage);
                        }
                    } else {
                        map.put("flag", false);
                        map.put("msg", "添加失败");
                        return JSON.toJSONString(map);
                    }
                }
            } else {
                map.put("flag", false);
                map.put("msg", "添加失败");
                return JSON.toJSONString(map);
            }
            map.put("flag", true);
            map.put("msg", "保存成功");
            return JSON.toJSONString(map);
        }
    }

    /**
     * @param student
     * @param session
     * @return 学员登陆
     */
    @Override
    public boolean login(Student student, StudentImage studentImage, HttpSession session) {
        Map map = new HashMap<>();
        map.put("idNumber", student.getIdNumber());
        map.put("idNo", studentImage.getIdNo());
        List<Student> users = baseMapper.login(map);
        //如果用户存在，放入session域
        if (users.size() > 0) {
            session.setAttribute("student", users.get(0));
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param idNumber
     * @return 个人赛是等级证书插叙
     */
    @Override
    public Student certificate(String idNumber) {
        return baseMapper.certificate(idNumber);
    }


    /**
     * @param id
     * @return 战队编辑回显city
     */
    @Override
    public Map getTeamCity(Long id) {
        String str = "";
        String sum = "";
        Map map = new HashMap();
        List<Student> list = baseMapper.getTeamCity(id);
        for (int i = 0; i < list.size(); i++) {
            str = list.get(i).getCity();
            sum += list.get(i).getStuId() + "," + list.get(i).getSubject() + "," + list.get(i).getSubjectLevel() + "," + list.get(i).getImageUrl() + "," + list.get(i).getIdNo() + "@";
        }
        map.put("str", str);
        map.put("sum", sum);
        return map;
    }

    @Override
    public Student countPerson() {
        return baseMapper.countPerson();
    }
    /**
     * @param student
     * @return 学员信息编辑
     */
    @Override
    public Map updateStudent(Student student, String param2) {
        if (this.updateById(student)) {
            String[] split = param2.split("@");
            for (int i = 0; i < split.length; i++) {
                String[] split1 = split[i].split(",");
                for (int j = 0; j < 1; j++) {
                    if (split[0].equals("undefined") || split[0] == "") {
                        break;
                    }
                    StudentImage studentImage = new StudentImage();
                    studentImage.setId(Long.parseLong(split1[0]));
                    studentImage.setSubject(Integer.parseInt(split1[1]));
                    studentImage.setSubjectLevel(Integer.parseInt(split1[2]));
                    studentImage.setIdNo(split1[3]);
                    if (!studentImageService.updateById(studentImage)) {
                        return ResponseUtil.getExceptionMap();
                    }
                }
            }
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }

    @Override
    public List<Student> getManyUrl(Map map) {

        List<Student> list = new ArrayList();
        //判断是身份证号 或 证件编号是否存在
        if (map.get("idNumber") != "" && map.get("idNumber") != null) {
            list = baseMapper.getImageUrl(map);
        }
        if (map.get("idNo") != "" && map.get("idNo") != null && list.size() == 0) {
            Student student = baseMapper.getidNumber((String) map.get("idNo"));
            map.put("idNumber",student.getIdNumber());
            list = baseMapper.getImageUrl(map);
        }
        return list;
    }

    @Override
    public Integer getCount(String idNumber,String idNo) {
        if (idNumber!=""){
            return baseMapper.getCount(idNumber);
        }
        Student student = baseMapper.getidNumber(idNo);
        return baseMapper.getCount(student.getIdNumber());
    }

    @Override
    public List<Student> getStudentCount(String idNumber,String idNo) {
        if (idNumber!=""){
            return baseMapper.getStudentCount(idNumber);
        }
        Student student = baseMapper.getidNumber(idNo);
        return baseMapper.getStudentCount(student.getIdNumber());
    }


//关联删除
    @Override
    public Integer deleteByIdStudent(Long id) {
        this.removeById(id);
        return baseMapper.deleteByIdStudent(id);
    }

}
